#include "titlebar.h"
#include<QHBoxLayout>
#include<QPalette>
#include <QGraphicsBlurEffect>
#include <QStyleOption>
#include <QMessageBox>
#include "custommessagebox.h"

#ifdef Q_OS_WIN
#pragma comment(lib, "user32.lib")
#include <qt_windows.h>
#endif
TitleBar::TitleBar(QWidget *parent) : QWidget(parent)
{
    setFixedHeight(55);//设置标题栏高度


    setAttribute(Qt::WA_TranslucentBackground);

    //设置文本粗体
    QFont &text_font = const_cast<QFont &>(font());
    text_font.setWeight(QFont::DemiBold);
    //创建图标以及标题
    iconLabel=new QLabel;
    iconLabel->setFixedSize(30, 30);//设置图标大小
    iconLabel->setScaledContents(true);//设置自动缩放
    iconLabel->setPixmap(QPixmap(tr(":/icons/leili.ico")));

    textLabel=new QLabel(tr("雷力-路径规划"));


    minBtn=new MyPushButton;
    closeBtn=new MyPushButton;
   // minBtn2,*menuBtn,*skinBtn,*vipBtn,*mailBtn;
    minBtn->setPicName(tr(":/title/title/min"));
    closeBtn->setPicName(tr(":/title/title/close"));
    minBtn2=new MyPushButton;
    minBtn2->setPicName(tr(":/title/title/min_arrow"));
    menuBtn=new MyPushButton;
    menuBtn->setPicName(tr(":/title/title/menu"));


    //水平布局依次添加Widget
    QHBoxLayout *hLayout=new QHBoxLayout(this);
    hLayout->addWidget(iconLabel);
    hLayout->setSpacing(1);
    hLayout->addSpacing(5);
    hLayout->addWidget(textLabel);
    hLayout->addStretch(10);

    hLayout->addStretch(10);;

    hLayout->addWidget(menuBtn);
    hLayout->addSpacing(15);
    hLayout->addWidget(minBtn2);
    hLayout->addWidget(minBtn);
    hLayout->addWidget(closeBtn);
    hLayout->setContentsMargins(5, 0, 10, 0);

    connect(minBtn,SIGNAL(clicked()),this,SLOT(onClicked()));
    connect(closeBtn,SIGNAL(clicked()),this,SLOT(onClicked()));
    connect(minBtn2,SIGNAL(clicked()),this,SLOT(onClicked()));
    connect(menuBtn,SIGNAL(clicked()),this,SIGNAL(ClickedMenuBtn()));


    menuBtn->installEventFilter(this);
    closeBtn->installEventFilter(this);
    minBtn2->installEventFilter(this);
    minBtn->installEventFilter(this);

    m_MaxNormal = true;
}

TitleBar::~TitleBar()
{

}

bool TitleBar::eventFilter(QObject *watched, QEvent *event)
{
    do{
        QMouseEvent* pEvent = static_cast<QMouseEvent *>(event);
        if(!pEvent)
        {
            break;
        }

        MyPushButton* btn = qobject_cast<MyPushButton*>(watched);
        if(!btn)
        {
            break;
        }

        if(pEvent->type() != QMouseEvent::MouseMove)
        {
            break;
        }

        if(btn == minBtn ||
                btn ==closeBtn ||
                btn == menuBtn ||
                btn == minBtn2)
        {
            return true;
        }
    }while(0);

    return QObject::eventFilter(watched, event);

}
void TitleBar::onClicked()
{
    QPushButton *pButton = qobject_cast<QPushButton *>(sender());
    QWidget *pWindow = this->window();
    if (pWindow->isTopLevel())
    {
        if (pButton == minBtn)
        {
            pWindow->showMinimized();
        }
        else if (pButton == closeBtn)
        {
            CustomMessageBox::ButtonRole ret = CustomMessageBox::question(nullptr, "提示", "是否退出?");
            if(ret != CustomMessageBox::Yes)
            {
                return;
            }

            pWindow->close();
        }
        else if(pButton == minBtn2)
        {
            showMaxRestore();
        }
    }
}
void TitleBar::mousePressEvent(QMouseEvent *e)//移动窗口
{
    m_StartPos = e->globalPos();
    m_ClickPos = mapToParent(e->pos());
}

void TitleBar::mouseMoveEvent(QMouseEvent *e)
{
    if (m_MaxNormal)  // 如果处于最大化状态，则不能移动
            return;
    parentWidget()->move(e->globalPos() - m_ClickPos);
}

void TitleBar::mouseDoubleClickEvent(QMouseEvent *e)
{
    if(e->button() == Qt::LeftButton)
    {
        showMaxRestore();
    }
}

void TitleBar::paintEvent(QPaintEvent *event)
{
    QStyleOption opt;
    opt.init(this);
    QPainter p(this);
    style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);
}


void TitleBar::showMaxRestore()
{
    if (m_MaxNormal) {
        parentWidget()->showNormal();
        minBtn2->setToolTip("最大化");
        minBtn2->setPicName(":/title/title/min_Max");
        setObjectName("titleBar");
        setStyle(this->style());
    } else {
        parentWidget()->showMaximized();
        minBtn2->setToolTip("向下还原");
        minBtn2->setPicName(tr(":/title/title/min_arrow"));
        setObjectName("titleBarMax");
        setStyle(this->style());
    }

    m_MaxNormal = !m_MaxNormal;
}
